package phone.example.com.a36kr.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.litesuits.orm.LiteOrm;
import com.litesuits.orm.db.assit.QueryBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;
import phone.example.com.a36kr.R;
import phone.example.com.a36kr.adapter.CollectLvAdapter;
import phone.example.com.a36kr.bean.LocalBean;
import phone.example.com.a36kr.bean.UserBean;
import phone.example.com.a36kr.nettool.NetTool;
import phone.example.com.a36kr.other.mybmob.MyBmobUser;
import phone.example.com.a36kr.tools.BaseActivity;
import phone.example.com.a36kr.tools.DBTool;

/**
 * Created by dllo on 16/8/30.
 */
public class MyCollectActivity extends BaseActivity {


    private ImageView iconIv;
    private String userId;
    private ListView lv;

    @Override
    protected int setLayout() {
        return R.layout.activity_collect_my;
    }

    @Override
    protected void initView() {
        // 头像
        iconIv = bindView(R.id.items_collect_icon);

        lv = bindView(R.id.collect_lv);

        ImageView backIv = bindView(R.id.collect_back);
        backIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {

        final MyBmobUser user = MyBmobUser.getCurrentUser(MyBmobUser.class);

        BmobQuery<UserBean> query = new BmobQuery<>();
        query.addWhereEqualTo("UserBean", user);
        query.setLimit(100);
        final LocalBean localBean = new LocalBean();
        query.findObjects(new FindListener<UserBean>() {
            @Override
            public void done(List<UserBean> list, BmobException e) {
                if (e == null) {
                    for (UserBean userBean : list) {
                        localBean.setTitle(userBean.getTitle());
                        Log.d("本地插入网络 简介", userBean.getTitle());
                        localBean.setContentUrl(userBean.getCollectUrl());
                        Log.d("本地插入网络 网址", userBean.getCollectUrl());
                        localBean.setIcon(userBean.getIcon());
                        localBean.setColumnName(userBean.getColumnName());
                        Log.d("MyCollectActivity", "网络数据插入本地成功");
                    }
                } else {
                    Log.i("bmob","失败："+e.getMessage()+","+e.getErrorCode());
                }
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        final CollectLvAdapter lvAdapter = new CollectLvAdapter(this);
        final MyBmobUser user = MyBmobUser.getCurrentUser(MyBmobUser.class);
        if (user != null) {

            LiteOrm liteOrm = LiteOrm.newSingleInstance(this, "MyDataBase.db");
            final ArrayList<LocalBean> arrayList = liteOrm.query(LocalBean.class);
            for (LocalBean bean : arrayList) {
                Log.d("收藏", "bean:" + bean);
                if (user.getUsername().equals(bean.getName())) {
                    final ArrayList<LocalBean> localBeen = new ArrayList<>();
                    localBeen.add(bean);
                    lvAdapter.setLocalBeen(arrayList);
                    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent = new Intent(MyCollectActivity.this, NewsSecondActivity.class);
                            intent.putExtra("curl", arrayList.get(position).getContentUrl());
                            Log.d("MyCollectActivity", arrayList.get(position).getContentUrl());
                            startActivity(intent);
                        }
                    });
                    lv.setAdapter(lvAdapter);

                }
            }
        } else {
            Toast.makeText(this, "请登录", Toast.LENGTH_SHORT).show();
        }
    }
}
